package com.codewsq;

import java.util.Arrays;

/*
 * 数组获取最大值、最小值以及平均值
 * */
public class 获取数组最大值最小值和平均值 {

    public static void main(String[] args) {
        int[] num = {23, 54, 6, 7, 2, 436, 87, 25};

        // 获取数组最大值
        int maxNum = 0;
        for (int i = 0; i < num.length; i++) {
            if (maxNum < num[i]) {
                maxNum = num[i];
            }
        }
        System.out.println("算法计算出来的数组最大值：" + maxNum);

        // 获取数组最小值
        int minNum = num[0];
        for (int i = 0; i < num.length; i++) {
            if (minNum > num[i]) {
                minNum = num[i];
            }
        }
        System.out.println("算法计算出来的数组最小值：" + minNum);

        // 获取数组平均值
        int sum = 0;
        for (int i = 0; i < num.length; i++) {
            sum += num[i];
        }
        double average = sum / num.length;
        System.out.println("算法计算出来的数组平均值：" + average);

        System.out.println("----------------------内置api返回结果-------------------------");

        System.out.println("内置api数组的最大值：" + Arrays.stream(num).max().getAsInt());
        System.out.println("内置api数组的最小值：" + Arrays.stream(num).min().getAsInt());
        System.out.println("内置api数组的平均值：" + Arrays.stream(num).average().getAsDouble());
    }
}
